using System;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
namespace Foundry.Graphics
{
	public static class DebugDraw
	{
		private static VertexArray<DebugVertex> va;
		private static DefaultShader shader;
		private static IUniform<Color4> CurrentColor;
		
		private static readonly string VertexShader = @"
#version 150

in vec3 Pos;

void main()
{
	gl_Position = vec4(Pos, 1) * ModelViewProjection;
}
";
		
		private static readonly string FragmentShader = @"
#version 150

out vec4 ColorOut;

uniform vec4 DebugColor;

void main()
{
	ColorOut = DebugColor;
}
";
		
		
		static DebugDraw()
		{
			va = new VertexArray<DebugVertex>();
			shader = new DefaultShader(
			                         Shader.FromSource<VertexShader>(VertexShader),
			                         Shader.FromSource<FragmentShader>(FragmentShader)
			                         );
			CurrentColor = shader.GetUniform<Color4>("DebugColor");
		}
		
		[Descriptor]
		private struct DebugVertex
		{
			public static readonly VertexDescriptor Descriptor = new VertexDescriptor(typeof(DebugVertex));
			public Vector3 Pos;
		}
		
		public static void DrawLine(Vector3 start, Vector3 stop, Color4 color)
		{
			shader.MakeCurrent();
			
			CurrentColor.Value = color;
			/*
			var mat = Matrix4.CreateTranslation(position) * Matrix4.Scale(scale)
				* Foundry.Engine.App.CurrentApplication.CurrentCamera.AbsoluteTransform
					* Foundry.Engine.App.CurrentApplication.CurrentCamera.Projection;
			
			shader.ModelViewProjection.Value = mat;
			
			
			va.MakeCurrent();
			
			va.Buffer.Allocate(2);
			va.Buffer[0] = start;
			va.Buffer[1] = stop;
			
			va.Buffer.BufferData(BufferUsageHint.StaticDraw);
			
			
			GL.DrawArrays(BeginMode.Lines,0,va.Buffer.Count);
			shader.MakeNullCurrent();*/
		}
		
		public static void DrawBoundingBox(Vector3 position, Vector3 scale, Color4 color)
		{
			shader.MakeCurrent();
			
			CurrentColor.Value = color;
			
			var mat = Matrix4.CreateTranslation(position) * Matrix4.Scale(scale)
				* Foundry.Engine.App.CurrentApplication.CurrentCamera.AbsoluteTransform
					* Foundry.Engine.App.CurrentApplication.CurrentCamera.Projection;
			
			shader.ModelViewProjection.Value = mat;
			
			
			
			va.MakeCurrent();
		}
		
	}
}

